package com.yuzhenxin.coursesystem.controller.admin;

import com.example.framework.common.pojo.R;
import com.example.framework.security.core.dto.JwtTokenRespDto;
import com.yuzhenxin.coursesystem.config.EndPoints;
import com.yuzhenxin.coursesystem.controller.admin.vo.UserLoginReqVO;
import com.yuzhenxin.coursesystem.controller.admin.vo.UserPasswordUpdateReqVO;
import com.yuzhenxin.coursesystem.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

    @Resource
    private AdminService adminService;

    @PermitAll
    @Operation(summary = "登录")
    @PostMapping(value = EndPoints.ADMIN_LOGIN, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<JwtTokenRespDto> login(@Valid @RequestBody UserLoginReqVO reqDTO) {
        JwtTokenRespDto jwtTokenRespDto = adminService.login(reqDTO);
        return R.success(jwtTokenRespDto);
    }

    @Operation(summary = "根据旧密码修改新密码")
    @PutMapping(value = EndPoints.ADMIN_UPDATEPASSWORD, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<String> updatePassword(@Valid @RequestBody UserPasswordUpdateReqVO reqDto) {
        adminService.updatePassword(reqDto);
        return R.success("密码修改成功");
    }
}
